import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Index from '../components/Index.vue'
import User from '../components/user/User.vue'
import Role from '../components/role/Role.vue'

Vue.use(VueRouter)

const routes = [

  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/home',
    name: 'home',
    component: Home,
    redirect: '/index',
    children: [
      { path: '/index', component: Index },
      { path: '/user/users', component: User },
      { path: '/role/roles', component: Role }
    ]
  }
]

const router = new VueRouter({
  mode: 'history', // 去掉地址的默认 #
  routes
})

// 路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  // const token = window.sessionStorage.getItem('token')
  // if (!token) return next('/login')
  next()
})

export default router
